package com.smooware.icms.adapter.list;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.smooware.icms.R;
import com.smooware.icms.data.model.Key;

public class WOKeyListAdapter extends ArrayAdapter<Key> {
	public WOKeyListAdapter(final Context context) {
		super(context, R.layout.wo_key_list_row, R.id.wo_key_number,
				new ArrayList<Key>());
	}

	public WOKeyListAdapter(final Context context, final List<Key> keys) {
		super(context, R.layout.wo_key_list_row, R.id.wo_key_number, keys);
	}

	@Override
	public View getView(final int position, final View convertView,
			final ViewGroup parent) {
		View view = super.getView(position, convertView, parent);

		Key key = getItem(position);

		initStatus(view, key);
		initLocation(view, key);
		initButton(view, key);

		return view;
	}

	private void initStatus(final View view, final Key key) {
		TextView statusView = (TextView) view.findViewById(R.id.wo_key_status);
		statusView.setText(key.getKeyStatus());
	}

	private void initLocation(final View view, final Key key) {
		TextView locationView = (TextView) view
				.findViewById(R.id.wo_key_location);
		locationView.setText(key.getKeyLocation());
	}

	private void initButton(final View view, final Key key) {
		Button actionButton = (Button) view.findViewById(R.id.wo_key_button);
		if (key.hasKey()) {
			actionButton.setText(getContext().getString(R.string.return_key));
		} else {
			actionButton.setText(getContext().getString(R.string.take_key));
		}
		actionButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				Log.d(WOKeyListAdapter.class.getSimpleName(), "touch");
			}
		});
	}
}
